package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.advertisment.model.NewRemainData;
import com.loongtech.bi.advertisment.service.AdvDayDataAllService;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: AdvDayAllDataAction
 *
 * @author liuchen
 * @Description: 市场数据分析 —— 广告日数据
 * @date 2017-03-28 下午2:23:49
 */

@Controller("adDayAllReportDetailAction")
@Scope("view")
public class AdvDayAllDataDetailAction extends BaseAdvAction {
	private static final long serialVersionUID = 1L;

    private String dateRadio = "7"; // 切换的天数：1=昨天。7=近7天。15=近15天。30=近30天，0=自选时间

    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY)); // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY)); // 自选时间：结束时间

    private List<NewRemainData> newRemainDatas;
    private List<List<Object>> totalPayDatas;

    @Resource(name = "advDayDataAllService")
    private AdvDayDataAllService service;

    @PostConstruct
    public void init() {
        search();
    }

    public void search() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (hasAgents == null || hasAgents.size() <= 0) {
            context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
            return;
        }
        if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
            context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
            return;
        }
        newRemainDatas = new ArrayList<>();
        totalPayDatas = new ArrayList<>();
        try {
            String startDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time
                    .currentTime() - (Integer.parseInt(dateRadio)) * Time.DAY))).getTime()))); // 开始时间
            String endDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time
                    .currentTime() - Time.DAY))).getTime()))); // 结束时间
            List<List<String>> stotalPayDatas =
                    service.getTotalPayDatas(startDay, endDay,
                            selectedAffiliation, selectedChannelName, selectedAgent,
                            selectedPlatform, selectedPaytype, selectedAdvPosition);
            for (List<String> ls : stotalPayDatas) {
                List<Object> lo = new ArrayList<>();
                for (int i = 0; i < ls.size(); i++) {
                    if (i < 1) {
                        lo.add(ls.get(i));
                    } else {
                        lo.add(Integer.valueOf(ls.get(i)));
                    }
                }
                totalPayDatas.add(lo);
            }

            newRemainDatas = service.getNewRemainDatas(startDay, endDay,
                    selectedAffiliation, selectedChannelName, selectedAgent,
                    selectedPlatform, selectedPaytype, selectedAdvPosition);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public List<NewRemainData> getNewRemainDatas() {
        return newRemainDatas;
    }

    public void setNewRemainDatas(List<NewRemainData> newRemainDatas) {
        this.newRemainDatas = newRemainDatas;
    }

    public List<List<Object>> getTotalPayDatas() {
        return totalPayDatas;
    }

    public void setTotalPayDatas(List<List<Object>> totalPayDatas) {
        this.totalPayDatas = totalPayDatas;
    }
}
